package gui;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import net.proteanit.sql.DbUtils;
import codes.DBBean;

public class UpdateCategory extends javax.swing.JFrame {

    public UpdateCategory() {
        initComponents();
    }


          private void initComponents() {

        	  jLabel3 = new javax.swing.JLabel();
              nameLabel = new javax.swing.JLabel();
              nameCategoryTextField = new javax.swing.JTextField();
              keywordsTextField = new javax.swing.JTextField();
              idParentTextField = new javax.swing.JTextField();
              titleWebPageTextField= new javax.swing.JTextField();
              keywordsLabel = new javax.swing.JLabel();
              idParentLabel = new javax.swing.JLabel();
              titleWebPageLabel = new javax.swing.JLabel();
              updateButton = new javax.swing.JButton();
              cleanButton = new javax.swing.JButton();
              returnButton = new javax.swing.JButton();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

              returnButton.setText("Return");
              returnButton.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                      try {
						returnButtonActionPerformed(evt);
					} catch (InstantiationException | IllegalAccessException
							| ClassNotFoundException | SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                  }
              });

              nameLabel.setText("Name");

              keywordsLabel.setText("Keywords");

              idParentLabel.setText("IdParent");

              titleWebPageLabel.setText("Title Web Page");
              
              nameCategoryTextField.setText(Category.nameUpdate);
              keywordsTextField.setText(Category.keywordsUpdate);
              idParentTextField.setText(Category.idParentUpdate);
              titleWebPageTextField.setText(Category.titleWebPageUpdate);
              
              updateButton.setText("Update");
              updateButton.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                      try {
						updateButtonActionPerformed(evt);
					} catch (InstantiationException | IllegalAccessException
							| ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                  }
              });

              cleanButton.setText("Clean");
              cleanButton.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                      cleanButtonActionPerformed(evt);
                  }
              });
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(95, 95, 95)
                            .addComponent(jLabel3))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(52, 52, 52)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(titleWebPageLabel)
                                .addComponent(idParentLabel)
                                .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(nameCategoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(titleWebPageTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(keywordsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(idParentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(61, 61, 61)
                                    .addComponent(cleanButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(returnButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(50, 50, 50)))))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(keywordsLabel)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(nameLabel)
                                .addGap(27, 27, 27)))
                        .addContainerGap(430, Short.MAX_VALUE)))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addComponent(nameCategoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(keywordsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(idParentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(idParentLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(titleWebPageLabel)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent( titleWebPageTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(43, 43, 43)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cleanButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(returnButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGap(78, 78, 78))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(nameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(keywordsLabel)
                        .addContainerGap(256, Short.MAX_VALUE)))
            );

            pack();
        }// </editor-fold>                                                   

    private void returnButtonActionPerformed(java.awt.event.ActionEvent evt) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {                                         
    	new Category().setVisible(true);
        UpdateCategory.this.dispose();
    }                                        

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) throws InstantiationException, IllegalAccessException, ClassNotFoundException {                                         
        try{
            DBBean db = new DBBean();
            Connection con = db.openConnection();

            String nomeCategoria = nameCategoryTextField.getText();
            String keywords = keywordsTextField.getText();
            String idParent = idParentTextField.getText();
            String titleWebPage = titleWebPageTextField.getText();
            try{
            	int value = Integer.parseInt(idParent);
            }catch (Exception e){
            	idParent = null;
            }

            String query = "UPDATE Category SET name = ?, keywords = ?, idParent = ?, titleWebPage = ? WHERE idCategory = ?";

            PreparedStatement statement = con.prepareStatement(query);

            statement.setString(1, nomeCategoria);
            statement.setString(2, keywords);
            statement.setString(3, idParent);
            statement.setString(4, titleWebPage);
            statement.setString(5, Category.idCategoryUpdate);

            statement.execute();

            JOptionPane.showMessageDialog(null, "Update Category with success! ");
            

        }catch(SQLException e){
            System.out.println(e);
            JOptionPane.showMessageDialog(null, "Error: Something is wrong!");
            
        }
    }                                        

    private void cleanButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        nameCategoryTextField.setText("");
        keywordsTextField.setText("");
        idParentTextField.setText("");
        titleWebPageTextField.setText("");
    }                                        

    
    public static void main(String args[]) {
       
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UpdateCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UpdateCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UpdateCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UpdateCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UpdateCategory().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton updateButton;
    private javax.swing.JButton cleanButton;
    private javax.swing.JButton returnButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel keywordsLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel idParentLabel;
    private javax.swing.JLabel titleWebPageLabel;
    private javax.swing.JTextField nameCategoryTextField;
    private javax.swing.JTextField keywordsTextField;
    private javax.swing.JTextField idParentTextField;
    private javax.swing.JTextField titleWebPageTextField;
    // End of variables declaration                   
}
